package com.example.polardbxdemo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.polardbxdemo.entity.Material;
import com.example.polardbxdemo.service.MaterialService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/material")
public class MaterialController {

    @Autowired
    private MaterialService materialService;

    /**
     * 添加测试物料
     */
    @PostMapping("/add")
    public String addMaterial(@RequestBody Material material) {
        boolean result = materialService.addMaterial(material);
        return result ? "添加成功" : "添加失败";
    }

    /**
     * 根据ID查询
     */
    @GetMapping("/{id}")
    public Material getById(@PathVariable Long id) {
        return materialService.getById(id);
    }

    /**
     * 查询所有物料
     */
    @GetMapping("/list")
    public List<Material> listAll() {
        return materialService.list();
    }

    /**
     * 根据类型查询
     */
    @GetMapping("/type/{type}")
    public List<Material> getByType(@PathVariable Integer type) {
        return materialService.getByType(type);
    }

    /**
     * 根据JSON属性查询
     */
    @GetMapping("/category/{category}")
    public List<Material> getByCategory(@PathVariable String category) {
        return materialService.getByPropertyCategory(category);
    }

    /**
     * 更新物料属性
     */
    @PutMapping("/{id}/properties")
    public String updateProperties(@PathVariable Long id, @RequestBody Map<String, Object> properties) {
        boolean result = materialService.updateMaterialProperties(id, properties);
        return result ? "更新成功" : "更新失败";
    }

    /**
     * 删除物料（逻辑删除）
     */
    @DeleteMapping("/{id}")
    public String delete(@PathVariable Long id) {
        boolean result = materialService.removeById(id);
        return result ? "删除成功" : "删除失败";
    }
    /**
     * 根据类型查询并按创建时间排序
     */
    @GetMapping("/type/{type}/ordered")
    public List<Material> getByTypeOrdered(@PathVariable Integer type) {
        return materialService.getByTypeOrderByCreateTime(type);
    }

    /**
     * 查询价格范围
     */
    @GetMapping("/price-range")
    public List<Material> getByPriceRange(
            @RequestParam(required = false) Double minPrice,
            @RequestParam(required = false) Double maxPrice) {
        return materialService.getByPriceRange(minPrice, maxPrice);
    }

    /**
     * 名称模糊查询
     */
    @GetMapping("/search/name")
    public List<Material> searchByName(@RequestParam String name) {
        return materialService.getByNameLike(name);
    }

    /**
     * 查询启用物料
     */
    @GetMapping("/enabled")
    public List<Material> getEnabledMaterials() {
        return materialService.getEnabledMaterials();
    }

    /**
     * 根据类型和库存查询
     */
    @GetMapping("/type-stock")
    public List<Material> getByTypeAndStock(
            @RequestParam(required = false) Integer type,
            @RequestParam(required = false) Integer minStock) {
        return materialService.getByTypeAndStock(type, minStock);
    }

    /**
     * 复杂条件查询
     */
    @GetMapping("/complex")
    public List<Material> complexQuery(
            @RequestParam(required = false) String name,
            @RequestParam(required = false) Integer type,
            @RequestParam(required = false) Double minPrice,
            @RequestParam(required = false) Double maxPrice,
            @RequestParam(required = false) Boolean enabled) {
        return materialService.complexQuery(name, type, minPrice, maxPrice, enabled);
    }
//
//    /**
//     * Lambda查询示例
//     */
//    @GetMapping("/lambda-search")
//    public List<Material> lambdaSearch(@RequestParam String keyword) {
//        return materialService.lambdaQueryExample(keyword);
//    }
//
//    /**
//     * 更新库存
//     */
//    @PutMapping("/{id}/stock")
//    public String updateStock(@PathVariable Long id, @RequestParam Integer stock) {
//        boolean result = materialService.updateStock(id, stock);
//        return result ? "库存更新成功" : "库存更新失败";
//    }
//
//    /**
//     * 批量更新启用状态
//     */
//    @PutMapping("/batch-enabled")
//    public String batchUpdateEnabled(@RequestParam List<Long> ids, @RequestParam Boolean enabled) {
//        boolean result = materialService.batchUpdateEnabled(ids, enabled);
//        return result ? "批量更新成功" : "批量更新失败";
//    }
    /**
     * 添加测试数据
     */
    @PostMapping("/init")
    public String initTestData() {
        // 测试数据1
        Map<String, Object> properties1 = new HashMap<>();
        properties1.put("category", "电子元件");
        properties1.put("brand", "品牌A");
        properties1.put("weight", 0.5);

        Map<String, Object> specs1 = new HashMap<>();
        specs1.put("voltage", "5V");
        specs1.put("current", "2A");
        specs1.put("size", "10x10mm");

        Material material1 = new Material();
        material1.setName("电阻器");
        material1.setCode("R001");
        material1.setType(1);
        material1.setPrice(0.5);
        material1.setStock(1000);
        material1.setEnabled(true);
        material1.setProperties(properties1);
        material1.setSpecifications(specs1);

        materialService.save(material1);

        // 测试数据2
        Map<String, Object> properties2 = new HashMap<>();
        properties2.put("category", "金属材料");
        properties2.put("brand", "品牌B");
        properties2.put("density", 7.85);

        Map<String, Object> specs2 = new HashMap<>();
        specs2.put("thickness", "2mm");
        specs2.put("hardness", "HRB 80");
        specs2.put("surface", "镀锌");

        Material material2 = new Material();
        material2.setName("钢板");
        material2.setCode("S001");
        material2.setType(2);
        material2.setPrice(25.8);
        material2.setStock(500);
        material2.setEnabled(true);
        material2.setProperties(properties2);
        material2.setSpecifications(specs2);

        materialService.save(material2);

        return "测试数据初始化完成";
    }
}